//
//  HJOderViewController.m
//  YaDiuDiu
//
//  Created by 胡光健 on 2017/10/29.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "HJOderViewController.h"
#import "HJOrderCell.h"
#import "HJGetOnModel.h"

#import "HJStatyView.h"
#import "HJOrderDetailViewController.h"


@interface HJOderViewController ()<UITableViewDelegate,UITableViewDataSource,statyViewDelegate> {
    UIView * _back;
}
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,strong) NSMutableArray * stayArr;
@property(nonatomic,strong) NSMutableArray * alreadyArr;
@property(nonatomic,strong) NSMutableArray * finishArr;
@property(nonatomic,assign)NSInteger page;

@property(nonatomic,strong)HJStatyView* stayView;
@end

@implementation HJOderViewController

-(NSMutableArray *)stayArr {
    if (!_stayArr) {
        _stayArr = [NSMutableArray array];
    }
    return _stayArr;
}
-(NSMutableArray *)finishArr {
    if (!_finishArr) {
        _finishArr = [NSMutableArray array];
    }
    return _finishArr;
}
-(NSMutableArray *)alreadyArr {
    if (!_alreadyArr) {
        _alreadyArr = [NSMutableArray array];
    }
    return _alreadyArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self showNav:self.navTitle];
    [self configTableView];
    [self loadRefresh];
    self.page = 1;
    if (self.type == stayType) {
        [self stayLoadData];
    }else {
        [self finishLoadData];
    }
}

-(void)configTableView {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.tableHeaderView = [UIView new];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.tableView registerCellByNibName:@"HJOrderCell"];
    [self.view addSubview:self.tableView];
}
//刷新控件
- (void)loadRefresh{
    //下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    //根据位置设置半透明
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    //上拉加载更多
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
}
-(void)loadData {
    self.page =1;
    if (self.type == stayType) {
        [self stayLoadData];
        
    }else {
        [self finishLoadData];
    }
    [self.stayArr removeAllObjects];
    [self.finishArr removeAllObjects];
    [self.alreadyArr removeAllObjects];
    [_tableView.mj_header endRefreshing];
}

-(void)loadMoreData {
    
    _page++;
    if (self.type == stayType) {
        [self stayLoadData];
    }else if (self.type== alreadyType) {
        [self finishLoadData];
    }
    [_tableView.mj_footer endRefreshing];
}
// 待接单
-(void)stayLoadData {
    NSString * url = [NSString stringWithFormat:@"%@%@",urlHead,@"/App/Ordermanage/wjontheorder"];
    [TZHttpTool get:url params:@{@"merchantid":self.merchantid,@"p":@(self.page)} success:^(NSDictionary *result) {
        NSArray * arr = [HJOrderModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
        if ([result[@"error"] isEqual:@200]) {
            [self.tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            return;
        }
        [self.stayArr addObjectsFromArray:arr];
        [self.tableView reloadData ];
        [_tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSString *error) {
        [LICGToast showWithText:error];
        [_tableView.mj_footer endRefreshing];
    }];
    
}

// 已完成
-(void)finishLoadData {
    NSString * url = [NSString stringWithFormat:@"%@%@",urlHead,@"/App/Ordermanage/wcontheorder"];
    [TZHttpTool get:url params:@{@"merchantid":self.merchantid,@"p":@(self.page)} success:^(NSDictionary *result) {
        NSArray * arr = [HJOrderModel mj_objectArrayWithKeyValuesArray:result[@"result"]];
        if ([result[@"error"] isEqual:@200]) {
            [self.tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            return;
        }
        [self.finishArr addObjectsFromArray:arr];
        [self.tableView reloadData ];
        [_tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSString *error) {
        [LICGToast showWithText:error];
        [_tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.type == stayType) {
        return [self.stayArr count];
    }else {
        return [self.finishArr count];
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HJOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HJOrderCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.type == stayType) {
        HJOrderModel * model = self.stayArr[indexPath.row];
        cell.model = model;
        WeakSelf
        cell.block = ^(NSString *merchant_id,NSString * chargesum,NSString * ischarge) {
            [weakSelf loadAlder:merchant_id chargesum:chargesum ischarge:ischarge];
        };
        cell.statueLabel.hidden = YES;
    }else {
        HJOrderModel * model = self.finishArr[indexPath.row];
        cell.model = model;
        cell.btn.hidden = YES;
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 160;
}
-(void)loadAlder:(NSString * )merchant_id chargesum:(NSString *)chargesum ischarge:(NSString *)ischarge {
    _back = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _back.backgroundColor = [UIColor blackColor];
    _back.alpha = 0.5;
    [[UIApplication sharedApplication].keyWindow addSubview:_back];
    
    self.stayView = [[HJStatyView alloc]init];
    self.stayView.frame =CGRectMake(25, self.view.center.y -70, ScreenWidth-50, 140);
    self.stayView.layer.cornerRadius = 8;
    self.stayView.layer.masksToBounds = YES;
    self.stayView.delegate = self;
    if ([ischarge isEqualToString:@"2"]) {
        self.stayView.conten.text = [NSString stringWithFormat:@"该订单收取%@元平台费,是否接受该订单",chargesum];
    }else {
        self.stayView.conten.text = @"是否接受该订单";
    }
    WeakSelf
    self.stayView.okBtnBlock = ^(UIButton *btn) {
        [_back removeFromSuperview];
        [weakSelf.stayView removeFromSuperview];
        
        NSString * url = [NSString stringWithFormat:@"%@%@",urlHead,@"/App/Ordermanage/affirmontheorder"];
        [TZHttpTool post:url params:@{@"merchantid":merchant_id} success:^(NSDictionary *result) {
            [LICGToast showWithText:@"已接单"];
        } failure:^(NSString *msg) {
            [LICGToast showWithText:msg];
        }];
        [weakSelf stayLoadData];
    };
    [[UIApplication sharedApplication].keyWindow addSubview:self.stayView];
    
}

-(void)cancleBtn {
    [_back removeFromSuperview];
    [self.stayView removeFromSuperview];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HJOrderDetailViewController * detail = [[HJOrderDetailViewController alloc]init];
    if (self.type == stayType) {
        HJOrderModel * model = self.stayArr[indexPath.row];
        detail.merchantid = model.merchant_id;
        detail.type = 1;
    }else {
        HJOrderModel * model = self.finishArr[indexPath.row];
        detail.merchantid = model.merchant_id;
        detail.type = 2;
    }
    detail.id_shop = self.merchantid;
    [self.navigationController pushViewController:detail animated:YES];
}
@end
